import type {ProColumns, ActionType} from '@ant-design/pro-table';
import {updateHospitalSet} from "@/services/hospital-set/api";

import {useState , useRef} from 'react'

export default function useHospitalSet( ) {
  /** 新建窗口的弹窗 */
  const [createModalVisible, handleModalVisible] = useState<boolean>(false);
  const [currentRow, setCurrentRow] = useState<API.HospitalSetItem>();
  const actionRef = useRef<ActionType>();
  const columns: ProColumns<API.HospitalSetItem>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      sorter: true,
    },
    {
      title: '医院名称',
      dataIndex: 'hosname',
    },
    {
      title: '医院编号',
      dataIndex: 'hoscode'
    },
    {
      title: 'apiUrl',
      dataIndex: 'apiUrl',
      hideInSearch: true,
    },
    {
      title: 'signKey',
      dataIndex: 'signKey',
      copyable: true,
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '联系人姓名',
      dataIndex: 'contactsName'
    },
    {
      title: '联系人号码',
      dataIndex: 'contactsPhone'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      valueType: 'dateTime',
      hideInSearch: true,
      sorter: true,
    }, {
      title: '更新时间',
      dataIndex: 'updateTime',
      valueType: 'dateTime',
      hideInSearch: true,
      sorter: true,
    },
    {
      title: "状态",
      dataIndex: 'status',
      filterMultiple: false,
      filters: true,
      onFilter: true,
      valueType: 'select',
      valueEnum: {
        0: {
          text: '锁定',
          status: 'Error',
        },
        1: {
          text: '正常',
          status: 'Success',
        }
      },
    },
    {
      title: "操作",
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="config"
          onClick={() => {
            setCurrentRow(record);
            handleModalVisible(true)
          }}
        >
          修改
        </a>,
        <a
          key="lock"
          onClick={async () => {
          const res = await  updateHospitalSet({
              id: record.id,
              status: record.status === 1 ? 0 : 1
            }, 'PUT')
            if(res.code === 200){
              actionRef?.current?.reload()
            }

          }}
        >
          {record.status === 0 ? '解锁' : '锁定'}
        </a>,

      ],
    },
  ];
  return {
    columns,
    createModalVisible, handleModalVisible,
    currentRow, setCurrentRow,
    actionRef
  }
}

